from nodetypes.ntypes.Document.view.RDF import Transform, Document
from nodetypes.Ns import GRAPHS_NS

class Document(Document):
    
    def getStatements(self, context, consumer):
        return self.getAgentProfileStatements(
            context,
            super(Document, self).getStatements(context, consumer)
        )  
           
    def getAgentProfileStatements(self, context, consumer):
        model = context.model
        href = context.server.graphs.get(GRAPHS_NS + 'agents')
        href = href and href.get('public') or None
        if href:
            agentDriver = model.Initialize(
                self.newAgentDriver(
                    context.repo,
                    href
                )
            )
        else:
            agentDriver = model.rdf_dbDriver
            
        mrs = model.rdf_toStatements        
        consumer(mrs('foafDepiction'))
        for n in model.foafDepiction:
            n.rdf_toStatement('dctermsModified', consumer)
        
        consumer(mrs('foafPrimaryTopic'))
        for n in model.foafPrimaryTopic:
            nrs = n.rdf_clone(
                agentDriver
            ).rdf_toStatements
            
            consumer(nrs('foafNick'))
            consumer(nrs('foafName'))
            consumer(nrs('foafMbox'))
            consumer(nrs('foafHomepage'))            
      
        return consumer


class Transform(Transform):
    
    newDocument = Document